package com.lei.yohuo001java;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;

import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lei.mvp_retrofitlibrary.mvp.view.BaseActivity;
import com.lei.yohuo001java.adapter.CommonAdapter;
import com.lei.yohuo001java.fragmentyouhuo.FourFragment;
import com.lei.yohuo001java.fragmentyouhuo.OneFragment;
import com.lei.yohuo001java.fragmentyouhuo.ThreeFragment;
import com.lei.yohuo001java.fragmentyouhuo.TwoFragment;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {
    private FrameLayout actFrag;
    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    private FourFragment fourFragment;
    private CommonTabLayout actCommonTabLayout;
    private ArrayList<CustomTabEntity> tabEntitys = new ArrayList<>();
    private ImageView actIvCar;

    @Override
    public int bindlayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        actCommonTabLayout = (CommonTabLayout) findViewById(R.id.act_common_tab_layout);
        actFrag = (FrameLayout) findViewById(R.id.act_frag);
        actIvCar = (ImageView) findViewById(R.id.act_iv_car);
        oneFragment = new OneFragment();
        twoFragment = new TwoFragment();
        threeFragment = new ThreeFragment();
        fourFragment = new FourFragment();
    }

    @Override
    public void initData() {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.act_frag,oneFragment)
                .add(R.id.act_frag,twoFragment)
                .add(R.id.act_frag,threeFragment)
                .add(R.id.act_frag,fourFragment)
                .commit();
        getfragment(oneFragment);
        tabEntitys.add(new CommonAdapter("首页",R.mipmap.radiobutton_home_select,R.mipmap.radiobutton_home_unselect));
        tabEntitys.add(new CommonAdapter("分类",R.mipmap.radiobutton_type_select,R.mipmap.radiobutton_type_unselect));
        tabEntitys.add(new CommonAdapter("市集",R.mipmap.radiobutton_shop_select,R.mipmap.radiobutton_shop_unselect));
        tabEntitys.add(new CommonAdapter("我的",R.mipmap.radiobutton_mine_select,R.mipmap.radiobutton_mine_unselect));
        actCommonTabLayout.setTabData(tabEntitys);
        actCommonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                switch (position){
                    case 0:
                        getfragment(oneFragment);
                        break;
                    case 1:
                        getfragment(twoFragment);
                        break;
                    case 2:
                        getfragment(threeFragment);
                        break;
                    case 3:
                        getfragment(fourFragment);
                        boolean log = SPUtils.getInstance().getBoolean("log");
                        if (log==true) {
                            FourFragment.tv1Four.setText("用户已登录");
                            FourFragment.bt1Four.setVisibility(View.VISIBLE);
                            FourFragment.bt2LogFour.setVisibility(View.GONE);
                        }else {
                            FourFragment.tv1Four.setText("用户已登出");
                            FourFragment.bt2LogFour.setVisibility(View.VISIBLE);
                            FourFragment.bt1Four.setVisibility(View.GONE);
                            startActivity(new Intent(MainActivity.this,LoginActivity.class));
                        }
                        break;
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        actIvCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,CarActivity.class));
            }
        });
    }

    private void getfragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction()
                .hide(oneFragment)
                .hide(twoFragment)
                .hide(threeFragment)
                .hide(fourFragment)
                .show(fragment)
                .commit();
    }
}
